package jjn.round1;

import commons.ListNode;

/**
 * @author Jjn
 * @since 2022/7/30 10:04
 */
public class LeetCode328_OddEvenLinkedList {

    public ListNode oddEvenList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode evenHead = head.next;
        ListNode odd = head, even = evenHead;
        while (even != null && even.next != null) {
            // 奇数节点的下一个必定是偶数节点
            odd.next = even.next;
            odd = odd.next;
            // 偶数节点的下一个必定是奇数节点
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
